/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */
package com.happycolor.littlelove.base;

import java.lang.reflect.Field;
import java.text.ParseException;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
/**
 * 
 * @author Panguso_af
 * @date 2012-9-26
 */
public final class Utils {
	private static  final HCLog log = HCLog.getInstance("Utils");
	private Utils() { }
	/**
	 * @param filePath 
	 */
	public static String generateGZIPFilePath(String filePath) {
		int index = filePath.lastIndexOf(".");
		String fileName = filePath.substring(0, index);
		return fileName + ".zip";
	}
/**
 * 
 * @param filePath 
 * @return
 */
	public static String getFileNameFromFilePath(String filePath) {
		if (filePath == null || filePath.length() < 1) {
			log.e("param is invalied, filePath: " + filePath);
			return "";
		}

		int index = filePath.lastIndexOf("/");
		if (index < 0 || index == filePath.length() - 1) {
			return "";
		}

		return filePath.substring(index + 1);
	}
/**
 * 
 * @param ctx 
 * @return
 */
	public static String getIMEI(Context ctx) {
		try {
			TelephonyManager tm = (TelephonyManager) ctx
			        .getSystemService(Context.TELEPHONY_SERVICE);
			String imei = tm.getDeviceId();
			if (imei == null || imei.length() < 1) {
				imei = Utils.getMobileMacAddr(ctx);
			}
			return imei;
		} catch (Exception ex) {
			log.e("getIMEI " + "Exception " + ex.toString());
		}
		return "";
	}
/**
 * 
 * @param context 
 * @return
 */
	public static String getMobileMacAddr(Context context) {
		WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		return info.getMacAddress();
	}
/**
 * 
 * @param filePath 
 * @return
 */
	public static String getParentPathFromFilePath(String filePath) {
		if (filePath == null || filePath.length() < 1) {
			log.e("param is invalied, filePath: " + filePath);
			return "";
		}

		int index = filePath.lastIndexOf("/");
		if (index < 0 || index == filePath.length() - 1) {
			return "";
		}

		return filePath.substring(0, index);
	}
/**
 * 
 * @param context 
 * @return
 */
	public static String getUA(Context context) {
		WebView webview = new WebView(context);
		webview.layout(0, 0, 0, 0);
		WebSettings settings = webview.getSettings();
		String ua = settings.getUserAgentString();

		return ua;
	}
/**
 * 
 * @param condition 
 * @return
 */
	public static String getWeatherCondition(String condition) {
		if (condition == null || condition.length() <= 1) {
			return "sunny";
		}

		if (condition.equalsIgnoreCase("sunny") || condition.equalsIgnoreCase("mostly sunny")
		        || condition.equalsIgnoreCase("partly sunny") || condition.equalsIgnoreCase("haze")// 薄雾
		        || condition.equalsIgnoreCase("Clear")) {
			return "sunny";
		} else if (condition.equalsIgnoreCase("partly cloudy")
		        || condition.equalsIgnoreCase("mostly cloudy")
		        || condition.equalsIgnoreCase("cloudy")) {
			return "cloudy";
		} else if (condition.equalsIgnoreCase("chance of storm")
		        || condition.equalsIgnoreCase("rain")
		        || condition.equalsIgnoreCase("chance of rain")
		        || condition.equalsIgnoreCase("thunderstorm")
		        || condition.equalsIgnoreCase("storm")
		        || condition.equalsIgnoreCase("scattered thunderstorms")
		        || condition.equalsIgnoreCase("showers")
		        || condition.equalsIgnoreCase("scattered showers")
		        || condition.equalsIgnoreCase("freezing drizzle")
		        || condition.equalsIgnoreCase("light rain") || condition.equalsIgnoreCase("hail")
		        || condition.equalsIgnoreCase("chance of tstorm")) {
			return "rain";
		} else if (condition.equalsIgnoreCase("mist") || condition.equalsIgnoreCase("dust")
		        || condition.equalsIgnoreCase("fog") || condition.equalsIgnoreCase("smoke")
		        || condition.equalsIgnoreCase("Overcast")) {
			return "mist";
		} else if (condition.equalsIgnoreCase("snow") || condition.equalsIgnoreCase("flurries")
		        || condition.equalsIgnoreCase("icy") || condition.equalsIgnoreCase("sleet")
		        || condition.equalsIgnoreCase("rain and snow")
		        || condition.equalsIgnoreCase("light snow")
		        || condition.equalsIgnoreCase("snow showers")
		        || condition.equalsIgnoreCase("chance of snow")) {
			return "snow";
		} else {
			return "sunny";
		}
	}
/**
 * 
 * @param minutes 
 * @param oldTime 
 * @return 
 * @throws ParseException 
 */
	public static boolean hasTimeElapsed(int minutes, long oldTime) throws ParseException {
		if (oldTime <= 0) {
			return true;
		}

		long oldMillis = oldTime;
		long curMillis = System.currentTimeMillis();

		if (Math.abs(curMillis - oldMillis) >= (long) minutes * 60 * 1000) {
			return true;
		}
			return false;
	}
/**
 * 
 * @param context 
 * @return
 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectMng = (ConnectivityManager) context
		        .getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] ntInfo = connectMng.getAllNetworkInfo();

		if (ntInfo != null) {
			for (int i = 0; i < ntInfo.length; i++) {
				if (ntInfo[i].getState() == NetworkInfo.State.CONNECTED) {
					log.i(ntInfo[i].getTypeName() + " is available");
					return true;
				}
			}
		}
		log.i("any network is inavailable");
		return false;
	}
/**
 * 
 * @param t 
 * @param msg 
 */
	public static void logError(Throwable t, String msg) {
		if (null != t) {
			log.i("" + msg + " " + t.getClass().getName() + " " + t.getMessage() + " "
			        + t.toString());
		} else {
			log.i("" + msg);
		}
	}
/**
 * 
 * @param context 
 * @param packageName 
 * @param resName 
 * @return
 */
	public static Drawable getThemeDrawable(Context context, String packageName, String resName) {
		int id = 0;
		try {
			Class<?> c = context.getClassLoader().loadClass(packageName + ".R");
			Class<?>[] cl = c.getClasses();
			for (int i = 0; i < cl.length; i++) {
				Field[] field = cl[i].getFields();
				for (int j = 0; j < field.length; j++) {
					if (field[j].getName().equals(resName)) {
						id = field[j].getInt(field[j].getName());
					}
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return context.getResources().getDrawable(id);
	}
	/**
	 * 获取手机版本号
	 * @return
	 * @date 2012-10-17
	 */
	public static int getAndroidSDKVersion() {
        int version = 0;
        try {
            version = Integer.valueOf(android.os.Build.VERSION.SDK);
        } catch (NumberFormatException e) {
        	e.printStackTrace();
        }
        return version;
    }
}